//
//  RootViewController_iPad.m
//  Agility
//
//  Created by Dominique d'Argent on 15.03.11.
//  Copyright 2011 Nicky Nubbel. All rights reserved.
//

#import "GameController_iPad.h"
#import "GameData.h"

@implementation GameController_iPad

@synthesize gameOverLabel;
@synthesize scoreLabel;
@synthesize startButton;

#pragma mark - Object lifecycle

//
// dealloc
//
// Releases retained objects.
//
- (void)dealloc {
    [gameOverLabel release];
    [scoreLabel release];
    [startButton release];
    
    [super dealloc];
}

//
// didReceiveMemoryWarning
//
// Frees some memory in case of a memory warning and hopefully saves the app
// from a crash.
//
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

//
// viewDidLoad
//
// Some view manipulations.
//
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // apply text shadow to labels
    
    gameOverLabel.layer.shadowOffset = CGSizeMake(2, -2);
    gameOverLabel.layer.shadowColor  = [UIColor blackColor].CGColor;
    gameOverLabel.layer.shadowRadius = 3.0;
    gameOverLabel.layer.opacity      = 1.0f;
    
    scoreLabel.layer.shadowOffset  = CGSizeMake(2, -2);
    scoreLabel.layer.shadowColor   = [UIColor blackColor].CGColor;
    scoreLabel.layer.shadowRadius  = 3.0;
    scoreLabel.layer.shadowOpacity = 1.0f;
}

//
// viewDidUnload
//
// Releases subview.
//
- (void)viewDidUnload {
    self.gameOverLabel = nil;
    self.scoreLabel    = nil;
    self.startButton   = nil;
    
    [super viewDidUnload];
}

#pragma mark - View manipulation

//
// gameOver
//
// Overridden to show a "Game Over!" message.
- (void)gameOver {
    gameOverLabel.hidden = NO;
    
    [NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(stop)
                                   userInfo:nil
                                    repeats:NO];
}

//
// gameStateChangedToState:
//
// Reacts to game state changes by modifying view components.
//
- (void)gameStateChangedToState:(GameState)newState {
    switch (newState) {
        case GameStateReady:
            startButton.hidden = NO;
            //scoreLabel.hidden = YES;
            gameOverLabel.hidden = YES;
            break;
        case GameStateRunning:
            startButton.hidden = YES;
            scoreLabel.hidden = NO;
            break;
        case GameStateStopped:
            gameOverLabel.hidden = NO;
            
            // shut down the game engine in a few seconds
            [NSTimer scheduledTimerWithTimeInterval:5.0
                                             target:self
                                           selector:@selector(stop)
                                           userInfo:nil
                                            repeats:NO];
            break;
    }
}

//
// updateScore
//
// Updates the score label to display the new score.
//
- (void)updateScore:(CGFloat)score {
    scoreLabel.text = [NSString stringWithFormat:@"Score: %.2f", score];
}

@end
